import matplotlib.pyplot as plt
import numpy as np
from scipy import signal

nmin = 0
nmax = 8
T = 0.01
n = np.arange(nmin, nmax + 1, 1)
nl = len(n)


def u(n):
   return np.where(n > 0, 1, 0)


# 定义系统模型
num = np.array([1.0, 1/5.0, 0.0])
den = np.array([1.0, 1.5, 1.0])

x = (0.5 ** n) * u(n)
y0 = np.array([2.0, 1.0])  # 零时刻状态

# 零状态响应
x01 = np.array([0])
zi1 = signal.lfilter_zi(num, den)  # 设置初始状态
y1, _ = signal.lfilter(num, den, x, zi=zi1 * x01)

# 零输入响应
x02 = np.zeros(nl)
zi2 = signal.lfiltic(num, den, y0)  # 设置初始状态
y2, _ = signal.lfilter(num, den, x02, zi=zi2)

# 全响应
y3, _ = signal.lfilter(num, den, x, zi=zi2)

plt.rcParams['font.sans-serif'] = ['SimHei']  # 加上这一句就能在图表中显示中文
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

plt.subplot(311)
plt.stem(n, y1)
plt.xlabel('n')
plt.ylabel('零状态响应')
# plt.ylim(-5, 5)

plt.subplot(312)
plt.stem(n, y2)
plt.xlabel('n')
plt.ylabel('零输入响应')
# plt.ylim(-5, 5)

plt.subplot(313)
plt.stem(n, y3)
plt.xlabel('n')
plt.ylabel('全响应')
# plt.ylim(-5, 5)



plt.show()




